
using System;
using System.Data;
using MySql.Data.MySqlClient;

namespace gtksharpdb
{
	
	
	public class MonoMysql : IDBFunction
	{
		private DBStatic dbs;
		private MySqlConnection mysqlconn;
		
		public MonoMysql(DBStatic mysqldbparameters)
		{
			dbs = mysqldbparameters;
		}
		
		#region IDBFunction implementation
		
		//Open a connection to MySQL
		public int DBConnection ()
		{
			   string connectionString =
	          "Server="+dbs.dbserver+";" +
	          "Database="+dbs.dbname+";" +
	          "User ID="+dbs.dbuser+";" +
	          "Password="+dbs.dbpin+";" +
	          "Pooling=false";
			
			try
			{
				mysqlconn = new MySqlConnection(connectionString);
				mysqlconn.Open();
				return 1;
			}
			catch (Exception ex)
			{
				ErrorHandler(ex);
				return -1;
			}
			
		}
		
		//Close connection
		public void DBClose ()
		{
			if(mysqlconn.State == ConnectionState.Open)
			{
				mysqlconn.Close();
			}
		}
		
		//Return query result in DataSet
		public DataSet DBQueryDS (string query)
		{
			DataSet ds = new DataSet();
			try
			{
				MySqlDataAdapter myda = new MySqlDataAdapter(query, mysqlconn);
				myda.Fill(ds);
				
				return ds;
			}
			catch (Exception ex)
			{
				ErrorHandler(ex);
				return null;
			}
		}
		
		//Return query result in DataTableReader
		public IDataReader DBQueryDR (string query)
		{
			try
			{
				MySqlCommand cmd = new MySqlCommand(query, mysqlconn);
				MySqlDataReader dr =  cmd.ExecuteReader();
				return dr;
			}
			catch (Exception ex)
			{
				ErrorHandler(ex);
				return null;
			}

		}
		
		//Execute SQL command
		public int DBCommand (string command)
		{
			try
			{
				MySqlCommand cmd = new MySqlCommand(command, mysqlconn);
				int i =  cmd.ExecuteNonQuery();
				return i;
			}
			catch (Exception ex)
			{
				ErrorHandler(ex);
				return -1;
			}
		}
		
		public void ErrorHandler (Exception ex)
		{
			throw new System.NotImplementedException();
		}
		
		#endregion
	}
}
